/*************************************************************
 * Title: 
 * Description: 
 * Author: Snow Huang
 * Email: Shaobin.Software@gmail.com Shaobin_Software@163.com
 * CreateTime: 2010-7-11 下午09:54:05
 * Copyright © 落羽社区 All right reserved
 ************************************************************/
package snow.core.web.listeners;

import java.io.File;
import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.ProxoolFacade;
import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;
import org.logicalcobwebs.proxool.configuration.PropertyConfigurator;

import snow.core.util.constants.ProxoolConfiguration;

/**
 * 
 */
public class ProxoolListenerConfigurator implements ServletContextListener {

	private final Log log = LogFactory.getLog(getClass()); // 日志类

	private boolean autoShutdown = true;

	/**
	 * @param sce
	 * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
	 */
	public void contextDestroyed(ServletContextEvent sce) {
		if (autoShutdown) {
			ProxoolFacade.shutdown(0);
		}
	}

	/**
	 * @param sce
	 * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
	 */
	public void contextInitialized(ServletContextEvent sce) {
		ServletContext context = sce.getServletContext();
		String appDir = context.getRealPath("/");
		Properties properties = new Properties();
		Enumeration<?> names = context.getInitParameterNames();
		while (names.hasMoreElements()) {
			Object temp = names.nextElement();
			if (temp != null && temp instanceof String) {
				String name = (String) temp;
				String value = context.getInitParameter(name);
				if (name.equals(ProxoolConfiguration.XML_FILE_PROPERTY)) {
					try {
						File file = new File(value);
						if (file.isAbsolute()) {
							JAXPConfigurator.configure(value, false);
						} else {
							JAXPConfigurator.configure(appDir + File.separator + value, false);
						}
					} catch (ProxoolException e) {
						log.error("Problem configuring " + value, e);
					}
				} else if (name.equals(ProxoolConfiguration.PROPERTY_FILE_PROPERTY)) {
					try {
						File file = new File(value);
						if (file.isAbsolute()) {
							PropertyConfigurator.configure(value);
						} else {
							PropertyConfigurator.configure(appDir + File.separator + value);
						}
					} catch (ProxoolException e) {
						log.error("Problem configuring " + value, e);
					}
				} else if (name.equals(ProxoolConfiguration.AUTO_SHUTDOWN_PROPERTY)) {
					autoShutdown = Boolean.valueOf(value).booleanValue();
				} else if (name.startsWith(ProxoolConfiguration.PREFIX)) {
					properties.setProperty(name, value);
				}
			}
		}
		if (properties.size() > 0) {
			try {
				PropertyConfigurator.configure(properties);
			} catch (ProxoolException e) {
				log.error("Problem configuring using init properties", e);
			}
		}
	}

}
